{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_vmywx8l"
   },
   "source": [
    "# Writing a setter function\n",
    "Test out the code below with an invalid date to see what happens when you use the `SetDate()` function to set the date. Add some logic to the `SetDate()` function to allow for February to have 29 days if it's a leap year!\n",
    "\n",
    "Scroll down and click the `Show Solution` button to see how to do this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "graffitiCellId": "id_30z956j",
    "graffitiConfig": {
     "executeCellViaGraffiti": "3hiwmz5_dulico3"
    }
   },
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "\n",
    "class Date {\n",
    "    // Declare member variables \n",
    "    public:\n",
    "        int day;\n",
    "        int month;\n",
    "        int year;\n",
    "        \n",
    "        // Declare member setter function\n",
    "        void SetDate(int day, int month, int year);   \n",
    "};\n",
    "\n",
    "// Define the SetDate() function\n",
    "void Date::SetDate(int day, int month, int year) \n",
    "{\n",
    "    int day_numbers[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n",
    "\n",
    "    // TODO: Your code goes here to check if input year is a leap year\n",
    "    // and if it is, give February 29 days \n",
    "    if (year % 4 == 0) {\n",
    "        day_numbers[1] = 29;\n",
    "    }\n",
    "\n",
    "    // Validate inputs\n",
    "    if(year < 1 || day < 1 || month < 1 || month > 12 || day > day_numbers[month - 1])\n",
    "        throw std::domain_error(\"Invalid date!\");\n",
    "\n",
    "    // Set date\n",
    "    Date::day = day; \n",
    "    Date::month = month; \n",
    "    Date::year = year;\n",
    "}\n",
    "\n",
    "// Test in main\n",
    "int main()\n",
    "{\n",
    "   Date date;\n",
    "   date.SetDate(29, 2, 2020);\n",
    "   std::cout << date.day << \"/\" << date.month << \"/\" << date.year << \"\\n\";\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_3hiwmz5"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_3hiwmz5-id_dulico3\"><i></i><button>Compile & Run</button></span>\n",
    "<span class=\"graffiti-highlight graffiti-id_1u9izv6-id_bifxawh\"><i></i><button>Explain</button></span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_8urr4x6",
    "graffitiConfig": {
     "rows": 6,
     "terminalId": "id_i52nu3z",
     "type": "terminal"
    }
   },
   "source": [
    "<i>Loading terminal (id_8urr4x6), please wait...</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "graffitiCellId": "id_gsm3sej"
   },
   "source": [
    "<span class=\"graffiti-highlight graffiti-id_gsm3sej-id_ziraq9o\"><i></i><button>Show Solution</button></span>"
   ]
  }
 ],
 "metadata": {
  "graffiti": {
   "firstAuthorId": "8978490740",
   "id": "id_shxo21s",
   "language": "EN"
  },
  "kernelspec": {
   "display_name": "C++11",
   "language": "C++11",
   "name": "xeus-cling-cpp11"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "-std=c++11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
